﻿using System.Collections.Generic;
using System.ComponentModel.Composition;
using InvestmentIntelligence.Files;
using InvestmentIntelligence.SchedulingService.Infrastructure;
using InvestmentIntelligence.TradingData.DataLoaders.LoadDataProcessor;
using InvestmentIntelligence.TradingData.DataLoaders.PostProcessing;
using InvestmentIntelligence.TradingData.DataLoaders.PostProcessing.TradingDataSecurity;
using InvestmentIntelligence.TradingData.DataLoaders.Rimes;
using InvestmentIntelligence.TradingData.DataProcessing.Processing;
using InvestmentIntelligence.TradingData.Handlers.Handlers.CsvHandlers;
using InvestmentIntelligence.TradingData.Handlers.Mus.FieldsConfiguration;
using InvestmentIntelligence.TradingData.TradingDataModels;

namespace InvestmentIntelligence.TradingData.Handlers.Mus
{
    [Export(typeof(IScheduledTaskFactory))]
    [ExportMetadata("TaskName", "LoadMusCsvBenchmarks")]
    public class CsvMusBenchmarkHandlerFactory : FolderBasedHandlerFactoryBase, IScheduledTaskFactory
    {
        private readonly ScheduledTaskParameter _benchmarkFolder = new StringScheduledTaskParameter(
            "benchmarks-dir", "specifies a benchmark  file");

        private readonly ScheduledTaskParameter _rimesUser = new StringScheduledTaskParameter(
            "rimes-user", "specifies a rimes user");

        private readonly ScheduledTaskParameter _rimesPassword = new StringScheduledTaskParameter(
            "rimes-password", "specifies a rimes password");


        public override IEnumerable<ScheduledTaskParameter> GetParameters()
        {
            yield return _benchmarkFolder;
            yield return _rimesUser;
            yield return _rimesPassword;
        }

        public override IScheduledTask CreateTask(CreateTaskParams createTaskParams, IDictionary<ScheduledTaskParameter, string> arguments)
        {

            var fileds = new BenchmarkFields();
            var securityDataLoader = new SecurityDataLoader(_rimesUser.Get(arguments, ""), _rimesPassword.Get(arguments, ""));
            var loadDataProcessor = new LoadDataProcessor<TradingDataSecurityDto>(securityDataLoader, InvestmentIntelligence.TradingData.AutomapperConfiguration.Configurations.Rimes.Mappings.TradingDataSecurityDto,
                                                                  new List<IDataPostProcessing<TradingDataSecurityDto>>
                                                                      {
                                                                          new RimesPostProcessingCurrencyTranslator()
                                                                      }, TradingDataSecurityDto.TradingDataSecurityDtoComparer);

            var files = FilteredFileReader.GetFiles(_benchmarkFolder.Get(arguments, ""), "*", "csv");
            return new CsvImportBenchmarksHandler(loadDataProcessor, files, fileds, Mappings.TradingDataBenchmarkDto, new SecurityResolver(SecurityResolver.AllIdentities, new UnresolvedSecurityWriter()));
        }
    }
}